/*
 * ===================================================
 *
 *       Filename:  exec2.c
 *
 *
 *        Created:  10/09/2011 02:25:31 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  durand_y, <durand.yoan@gmail.com>
 *
 * ===================================================
 */
#include "find.h"

s_list      *exec_name(s_list    *list_op,
                       s_list    **list)
{
    if(test_next(list_op) == 1)
        exit (name(list, list_op->next->dir, 1));
    else
        name(list, list_op->next->dir, 0);
    return (list_op->next->next);
}

s_list      *exec_newer(s_list   *list_op,
                        s_list   **list)
{
    if (test_next(list_op) == 1)
        exit (newer(list, list_op->next->dir, 1));
    else
        newer(list, list_op->next->dir, 0);
    return (list_op->next->next);
}

static int  is_int(char  *s)
{
    int     i = 0;
    for (; ((s[i] >= '0') && (s[i] <= '9') && (s[i] != '\0')); i++)
        ;
    if (s[i] == '\0')
        return (1);
    else
        return (0);
}

s_list      *exec_atime(s_list   *list_op,
                        s_list   **list)
{
    char    *s = NULL;

    if (is_int(list_op->next->dir) == 0)
    {
        s = mystrcat("./find: invalid argument `", list_op->next->dir);
        s = mystrcat(s, "' to `");
        s = mystrcat(s, list_op->dir);
        s = mystrcat(s, "'\n");
        write(STDOUT_FILENO, s, mystrlen(s));
        exit (1);
    }
    if (test_next(list_op) == 1)
        exit (atime(list, my_atoi(list_op->next->dir), 1));
    else
        atime(list, my_atoi(list_op->next->dir), 0);
    return (list_op->next->next);
}

void        exec_error(char     *s,
                       char     *str)
{
    my_error(s, str);
    exit (1);
}

void        exec_help(char      *s)
{
    usage(s);
    exit(0);
}
